package com.zzl.alligator.management.vote.controller;

import cn.hutool.core.lang.Assert;
import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.common.util.ServletUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.security.service.TokenService;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysUser;
import com.zzl.alligator.management.vote.domain.VoteSubject;
import com.zzl.alligator.management.vote.service.IVoteSubjectService;
import com.zzl.alligator.management.vote.vo.VoteGuideVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投票主题(VoteSubject)控制层
 *
 * @author zhouziliang
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/vote/subject")
@Slf4j
public class VoteSubjectController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private IVoteSubjectService voteSubjectService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询所有数据
     *
     * @param voteSubject 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(VoteSubject voteSubject) {
        startPage();
        List<VoteSubject> voteSubjectList = voteSubjectService.findVoteSubjectList(voteSubject);
        return JsonResult.success(getDataTable(voteSubjectList));
    }

    /**
     * 分页查询本人创建的所有数据
     *
     * @param voteSubject 查询实体
     * @return 所有数据
     */
    @GetMapping("/mine")
    public JsonResult findMyList(VoteSubject voteSubject) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        voteSubject.setCreateBy(user.getUserName());
        startPage();
        List<VoteSubject> voteSubjectList = voteSubjectService.findVoteSubjectList(voteSubject);
        return JsonResult.success(getDataTable(voteSubjectList));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param subjectId 主键
     * @return 单条数据
     */
    @GetMapping("/{subjectId}")
    public JsonResult findById(@PathVariable Long subjectId) {
        return JsonResult.success(voteSubjectService.findVoteSubjectById(subjectId));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param subjectId 主键
     * @return 单条数据
     */
    @GetMapping("/guide/{subjectId}")
    public JsonResult findSubjectGuideById(@PathVariable Long subjectId) {
        return JsonResult.success(voteSubjectService.findVoteSubjectGuideById(subjectId));
    }


    /**
     * 新增数据
     *
     * @param voteSubject 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增投票主题", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody VoteSubject voteSubject) {
        return toAjax(this.voteSubjectService.insertVoteSubject(voteSubject));
    }

    /**
     * 新增数据
     *
     * @param voteGuideVO 实体对象
     * @return 新增结果
     */
    @PostMapping("/guide")
    @Log(title = "新增投票主题", businessType = BusinessTypeEnum.INSERT)
    public JsonResult subjectGuide(@RequestBody VoteGuideVO voteGuideVO) {
        voteSubjectService.insertVote(voteGuideVO);
        return JsonResult.success();
    }

    /**
     * 修改数据
     *
     * @param voteSubject 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Log(title = "更新投票主题", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody VoteSubject voteSubject) {
        return toAjax(this.voteSubjectService.updateVoteSubject(voteSubject));
    }

    @PutMapping("/guide/start/{subjectId}")
    @Log(title = "启动投票", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult startSubject(@PathVariable Long subjectId) {
        return toAjax(this.voteSubjectService.startSubject(subjectId));
    }
    /**
     * 删除数据
     *
     * @param subjectIds 主键
     * @return 删除结果
     */
    @DeleteMapping("/{subjectIds}")
    @Log(title = "删除投票主题", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable("subjectIds") String subjectIds) {
        Assert.notNull(subjectIds);
        List<Long> idList = Arrays.stream(subjectIds.split(",")).map(Long::new).collect(Collectors.toList());
        return toAjax(voteSubjectService.deleteByIds(idList));
    }
    
    /**
     * 导出数据
     * 
     * @param voteSubject 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出投票主题", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(VoteSubject voteSubject) {
        List<VoteSubject> list = voteSubjectService.findVoteSubjectList(voteSubject);
        ExcelUtils<VoteSubject> util = new ExcelUtils<>(VoteSubject.class);
        return util.exportExcel(list, "投票主题数据");
    }
}